package com.example.crm.utils;
import java.sql.*;

public class JDBCUtils {// 定义连接数据库的信息
//	static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//	static final String DB_URL = "jdbc:mysql://localhost:3306/test_demo?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
//	// 数据库的用户名与密码，需要根据自己的设置
//	static final String USER = "root";
//	static final String PASS = "root";
//

	public static final String URL = "jdbc:mysql://localhost:3306/crm";
	public static final String USERNAME = "root";
	public static final String PASSWORD = "root";
	static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
	static Connection conn = null;
	static Statement stmt = null;
	// 0. 加载驱动
    static
    {
        try {
			//Class.forName("com.mysql.jdbc.cj.Driver");
           Class.forName(JDBC_DRIVER);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

	// 1. 连接数据库
	public static Connection getConnection() throws SQLException
	{
		conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);

		return conn;
	}

	// 2. 释放资源
	public static void destory(Connection conn,Statement stmt,ResultSet rs)
	{
		if(rs!=null)
		{
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(stmt!=null)
		{
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(conn!=null)
		{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}